//
//  UIView+ProgressHUB.m
//  Manager
//
//  Created by scho on 2018/11/17.
//  Copyright © 2018 scho. All rights reserved.
//

#import "UIView+ProgressHUB.h"
#import <MBProgressHUD/MBProgressHUD.h>

@implementation UIView (ProgressHUB)

#pragma mark - Show MBProgressHUD
- (void)showHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI
        UIView *lockView = self;
        
        //[MBProgressHUD hideAllHUDsForView:lockView animated:NO]; // hide all first, avoid lock issue.
        [MBProgressHUD hideHUDForView:lockView animated:NO];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:lockView animated:YES];
        
        // shown using an UIActivityIndicatorView.
        hud.mode = MBProgressHUDModeIndeterminate;
    });
}

- (void)hideHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI
        UIView *lockView = self;
        //[MBProgressHUD hideAllHUDsForView:lockView animated:YES];
        [MBProgressHUD hideHUDForView:lockView animated:YES];
    });
}

- (void)showHUDWithText:(NSString*)text
{
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI
        UIView *lockView = self;
        
        //[MBProgressHUD hideAllHUDsForView:lockView animated:NO];
        [MBProgressHUD hideHUDForView:lockView animated:NO];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:lockView animated:YES];
        
        // Configure for text only and offset down
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.label.font = [UIFont systemFontOfSize:14];
        hud.margin = 18.f;
        hud.contentColor = [UIColor whiteColor];
        //hud.bezelView.backgroundColor = HexColor(0x333333);
        hud.bezelView.backgroundColor = [UIColor blackColor];
        hud.removeFromSuperViewOnHide = YES;
    });
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [MBProgressHUD hideHUDForView:self animated:YES];
    });
}

- (void)showHUDWithText:(NSString*)text delayHide:(NSTimeInterval)dalay
{
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI
        UIView *lockView = self;
        
        //[MBProgressHUD hideAllHUDsForView:lockView animated:NO];
        [MBProgressHUD hideHUDForView:lockView animated:NO];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:lockView animated:YES];
        
        // Configure for text only and offset down
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.label.font = [UIFont systemFontOfSize:14];
        hud.margin = 18.f;
        hud.contentColor = [UIColor whiteColor];
        //hud.bezelView.backgroundColor = HexColor(0x333333);
        hud.bezelView.backgroundColor = [UIColor blackColor];
        hud.removeFromSuperViewOnHide = YES;
    });
    
    // default 1 second
    if (dalay < 0.01) {
        dalay = 1.0;
    }
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, dalay * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [MBProgressHUD hideHUDForView:self animated:YES];
    });
}

// check data is good or bad by status, whether show tip HUB.
- (BOOL)checkResponseData:(NSDictionary*)info showTip:(BOOL)show {
    // debug log
    
    if (![info isKindOfClass:[NSDictionary class]]) {
        if (show) {
            NSString *tipString = @"数据出现异常";
            if ([info isKindOfClass:[NSError class]]) {
                tipString = @"服务器接口出现异常";
            }
            [self showHUDWithText:tipString delayHide:1.0];
        }
        return NO;
    }
    
    BOOL flag = NO;
    NSNumber *flagNumber = info[@"flag"];
    if ([flagNumber isKindOfClass:[NSNumber class]]) {
        flag = [flagNumber boolValue];
    }
    
    if (flag) {
        // Good data
        return YES;
    }
    else if (show){
        // Bad data
        
        // show tip
        NSString *tipString = @"数据出现异常";
        NSString *msg = info[@"msg"];
        if ([msg isKindOfClass:[NSString class]]) {
            tipString = msg;
        }
        [self showHUDWithText:tipString delayHide:1.0];
        
        return NO;
    }
    else {
        // Bad data
        return NO;
    }
    
    return NO;
}

@end
